/**
 * 
 */
package cn.edu.pku.sei.ra.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.EditorReference;

import ac.jp.u_tokyo.QVTWrapper.SynchronizationFailedException;

import cn.edu.pku.sei.ra.framework.CausalConnector;

/**
 * @author nishimura
 *
 */
public class CausalConnectorRunnerAction extends
		Action {
	
	public static List<CausalConnector> ALL_CAUSAL_CONNECTORS=new ArrayList<CausalConnector>();
	
	private CausalConnector connector=null;
	private IProject project;
	/**
	 * 
	 */
	public CausalConnectorRunnerAction(CausalConnector connector, IProject project) {
		this.connector=connector;
		this.project=project;
		ALL_CAUSAL_CONNECTORS.add(connector);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
	 */
	public void init(IWorkbenchWindow window) {
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		try {
			connector.maintain();
			try{
				project.refreshLocal(2, null);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			MessageDialog.openInformation(null, "Error", e.toString());
		}

	}
	
	public void run() {
		try {
			
			IEditorReference[] refs=PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage().getEditorReferences();
			for(int i=0;i<refs.length;i++){
				IEditorPart editor=refs[i].getEditor(false);
				if(editor!=null && editor.isDirty()){
					editor.doSave(null);
				}
			}
			
			connector.maintain();
			
			project.refreshLocal(2, null);
			
			MessageDialog.openInformation(null, 
					"Done", 
					"Runtime synchronization performed successfully");
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			MessageDialog.openInformation(null, "Error", e.toString());
		}
		finally{
			connector.revertGloblePackageRegister();
		}

	}
	

	

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
	}
	
	public String getText(){
		return connector.toString();
	}

}
